/**
 *
 */
package com.feizhi.common.repo.helper.impl;

import com.feizhi.common.repo.helper.SqlUtil;

/**
 * @author shuaqiu 2013年11月16日
 *
 */
public class MysqlUtil implements SqlUtil {

    @Override
    public String escapeLikeValue(String likeValue) {
        likeValue = likeValue.replaceAll("'", "''");
        likeValue = likeValue.replaceAll("\\[", "[[]");
        likeValue = likeValue.replaceAll("%", "[%]");
        likeValue = likeValue.replaceAll("\\_", "[_]");
        return likeValue;
    }

    @Override
    public String toRangeQuerySql(final String sql, final long[] range) {
        return toRangeQuerySql(sql, "", range);
    }

    @Override
    public String toRangeQuerySql(final String sql, final String orderBy,
            final long[] range) {
        if (range != null && range.length >= 2) {
            final long offset = range[0] - 1;
            final long count = range[1] - offset;
            return "select t.* from (" + sql + ") t " + orderBy
                    + " limit " + offset + ", " + count;
        }
        return sql;
    }

}
